<template>
  <a-modal
    title="添加项目"
    :width="810"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="12">
          <a-col :md="24" :sm="24">
            <a-form-item
              label="项目名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input v-decorator="['projectName', {rules: [{required: true, message: '请输入项目名称', trigger: 'blur'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24">
            <a-form-item
              label="投票类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              @change="handleChange"
            >
              <a-select v-decorator="['projectType', {rules:[{required: true, message: '请选择投票类型'}]}]">
                <a-select-option :value="0">选择评价型</a-select-option>
                <a-select-option :value="1">同意反对型</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" v-if="this.form.getFieldsValue().projectType === 1">
            <a-form-item
              label="最大票数"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input v-decorator="['maxVote', {rules: [{required: true, message: '请输入最大票数', trigger: 'blur'}]}]" oninput="value=value.replace(/[^\d]/g,'')" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24">
            <a-form-item
              label="评审日期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-date-picker valueFormat="YYYY-MM-DD"
                             v-decorator="['date',{rules: [{required: true, message: '请选择投票日期', trigger: 'blur'}]}]" @change="onChange" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { addProject } from '@/api/vote/project'
import { querySubject, queryType } from '../../../api/vote/project'

export default {
  props: {
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,
      queryParam: {},
      Type: [],
      Subject: [],
      voteType: 0,
      form: this.$form.createForm(this)
    }
  },
  created() {
    this.listType()
    this.listSubject()
  },
  methods: {
    handleChange(value) {
      console.log(value)
      this.voteType = value
      console.log(this.voteType)
    },
    listType() {
      const that = this
      this.$nextTick().then(function () {
        const queryJson = {
          'pageNo': 1,
          'pageSize': 10,
          'queryParam': JSON.stringify(that.queryParam)
        }
        queryType(queryJson).then(res => {
          for (let i = 0; i < res.result.data.length; i++) {
            that.Type[i] = res.result.data[i].typeName
          }
          console.log(that.Type)
        })
      })
    },
    listSubject () {
      const that = this
      this.$nextTick().then(function () {
        const queryJson = {
          'pageNo': 1,
          'pageSize': 10,
          'queryParam': JSON.stringify(that.queryParam)
        }
        querySubject(queryJson).then(res => {
          for (let i = 0; i < res.result.data.length; i++) {
            that.Subject[i] = res.result.data[i].subjectName
          }
        })
      })
    },
    onChange(date, dateString) {
      console.log(date, dateString)
    },
    add () {
      this.visible = true
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          console.log(values)
          addProject(values)
            .then(res => {
              if (res.message !== 'error') {
                this.visible = false
                this.confirmLoading = false
                this.form.resetFields()
                this.$emit('ok')
              } else {
                console.log('addOk!')
                this.confirmLoading = false
                this.$message.error('操作失败！')
              }
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
